package outputstream;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;

public class ObjectOutStream {

    public static void main(String[] args) throws Exception{
        String filePath = "D:/IO/student.txt";
        ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream(filePath));

        //保存一个对象
        Scanner sc = new Scanner(System.in);
        Student stu = new Student();
        System.out.println("请输入学生年龄");
        stu.setAge(sc.nextInt());
        System.out.println("请输入学生姓名");
        stu.setName(sc.next());
        System.out.println("请输入学生班级");
        stu.setClassroom(sc.next());

       // oos.writeObject(new Student("张三","一年三班",18));
        oos.writeObject(stu);
        oos.close();
        System.out.println("数据保存完毕");

    }
}
//从控制台输入一位学生的姓名、班级、年龄并保存到student.txt文件中
class  Student implements Serializable {
            private String name;
            private String classroom;
            private int age;

    public Student() {
    }

    public Student(String name, String classroom, int age) {
        this.name = name;
        this.classroom = classroom;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getClassroom() {
        return classroom;
    }

    public void setClassroom(String classroom) {
        this.classroom = classroom;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}